<template>
  <el-container class="home-container">
    <!-- Header区域 -->
    <el-header>
      <div>
        <img src="../assets/renko.jpg" alt="/"> <span>电商后台管理系统</span>
      </div>
      <el-button type="danger" size="default" @click="LoginOut"
        >注销账户</el-button
      >
    </el-header>

    

    <!-- 页面主体 -->
    <el-container>
      <!-- 左侧菜单区域 -->
      <el-aside :width="isCollapse ? '64px' : '240px'">
        <div class="toggle-button" @click="toggle_button">|||</div>

        <el-col :span="24">
          <el-menu
            @open="handleOpen"
            @close="handleClose"
            background-color="#303643"
            text-color="#fff"
            active-text-color="#3F9CFF"
            :unique-opened="true"
            :collapse="isCollapse"
            :router="true"
            :default-active="activePath"
          >
          <!-- :router="ture是启用按index获得的值，跳转路由" -->
            <!-- unique-opened用于只展开单个一级菜单，点击其他的就会把其他展开的缩回去 -->
            <!-- 一级菜单模板 -->
            <!-- index中只能接受字符串，然而item.id不是字符串，所以增加一个''空字符串+item.id -->
            <el-submenu
              :index="'' + item.id"
              v-for="item in menuList"
              :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
               @click="saveNavState('/'+subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-col>
      </el-aside>
      <!-- Main区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
      
  data() {
    return {
      menuList: [],
      // 左侧icon表
      iconObj: {
        125: "el-icon-user-solid",
        103: "el-icon-files",
        101: "el-icon-s-goods",
        102: "el-icon-s-order",
        145: "el-icon-s-data",
      },
      isCollapse: false,
      activePath:""
    };
  },
  methods: {
    // 注销，把sessionStorage的token清空
    LoginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 侧边栏
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    // async异步发送Ajax get请求请求后端服务器数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      console.log(res);
      if (res.meta.status !== 200) return this.$message("未获取到数据");
      else if (res.meta.status === 200) {
        this.menuList = res.data;
        console.log(this.menuList);
        return this.$message("获取到数据");
      }
    },

    // 切换左侧菜单栏伸长缩短
    toggle_button() {
      this.isCollapse = !this.isCollapse;
      console.log(this.isCollapse);
    },
    // 保存连接激活状态，这里存放在sessionStorage
     saveNavState(data){
      window.sessionStorage.setItem('activePath',data)
     
     }
  },
  created() {
    this.getMenuList()
    this.activePath=window.sessionStorage.getItem('activePath')
    console.log("aaaa"+this.activePath);
  },
 
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  background-color: #409eff;
  align-items: center;
  > div {
    display: flex;
    height: 100%;
    align-items: center;
    border-radius: 50%;
  >img {
    border-radius: 50%;
      height: 100%;
    }
    span {
      font-size: 20px;
      font-family: "Micosoft Yahei";
      color: #fff;
      cursor: default;
    }
  }
}

.el-aside {
  background-color: #303643;

  .el-menu {
    border-right-color: #303643;
  }
}

.el-main {
  background-color: #e9edf0;
}

.toggle-button {
  background-color: #485162;
  font-size: 14px;
  color: #ccc;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  &:hover {
    background-color: #303643;
  }
 
}
</style>